﻿using System;
using System.Collections.Generic;
using System.Linq;
using BL.Model;
using BL.Model.Request;
using Common;
using DAL.Model;

namespace BL
{
    /// <summary>
    /// Controller for receipt
    /// </summary>
    /// <history>
    /// 12/05/2014 phat.vo: Create new
    /// </history>
    public class ReceiptController : BaseController, IReceiptController
    {
        private readonly MasterSession _session;

        #region Ctor

        public ReceiptController(MasterSession session)
        {
            _session = session;
        }
        
        #endregion

        public void Create()
        {
            throw new NotImplementedException();
        }

        public void View()
        {
            throw new NotImplementedException();
        }

        public void Update()
        {
            throw new NotImplementedException();
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public void Print()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Create new item
        /// </summary>
        /// <param name="item"></param>
        /// <history>
        /// 12/05/2014 phat.vo: Create new
        /// </history>
        public void Create(Receipt item)
        {
            _session.Receipts.Add(item);
            _session.Liabilities.Add(new Liability
                                           {
                                               CustomerId = item.CustomerId,
                                               Description = item.Desciption,
                                               IssuedDate = item.ReceiptedDate,
                                               Receipt = item.TotalAmount,
                                               Discount = item.AllowanceCharge,
                                               Receipt1 = item
                                           });
        }

        /// <summary>
        /// Search product category by keyword with paging and ordering
        /// </summary>
        /// <param name="request"></param>
        /// <returns>List of product category</returns>
        /// <history>
        /// 11/30/2014 phat.vo: Create new
        /// </history>
        public List<ReceiptDetail> Search(SearchingReceiptRequest request)
        {
            var isAllCustomer = request.CustomerIds == null || request.CustomerIds.Contains(Constant.AllValue);
            var customerIds = isAllCustomer ? new List<int>() : request.CustomerIds;

            var isAllId = request.Ids == null || request.Ids.Contains(Constant.AllValue);
            var ids = isAllId ? new List<int>() : request.Ids;

            var query =
                (from receipt in _session.Receipts
                 where (string.IsNullOrEmpty(request.DocumentNo) || request.DocumentNo.Trim() == receipt.DocumentNo.Trim()) &&
                       (!request.FromAmount.HasValue || request.FromAmount == 0 || request.FromAmount == receipt.TotalAmount) &&
                       (!request.FromTime.HasValue || request.FromTime <= receipt.ReceiptedDate) &&
                       (!request.ToTime.HasValue || request.ToTime >= receipt.ReceiptedDate) &&
                       (isAllCustomer || customerIds.Contains(receipt.CustomerId))
                 select new ReceiptDetail
                            {
                                Id = receipt.Id,
                                CustomerId = receipt.CustomerId,
                                Desciption = receipt.Desciption,
                                Payer = receipt.Payer,
                                CustomerName = receipt.Customer.Name,
                                ReceiptAmount = receipt.ReceiptAmount ?? 0,
                                TotalAmount = receipt.TotalAmount ?? 0,
                                DiscountAmount = receipt.AllowanceCharge ?? 0,
                                ReceiptedDate = receipt.ReceiptedDate,
                                Receiver = receipt.Receiver,
                                StockOutId = receipt.StockOutId,
                                DocumentNo = receipt.DocumentNo,
                            })
                    .OrderByDescending(x => x.ReceiptedDate);

            return query.ToList();
        }

        /// <summary>
        /// Get receipt detail
        /// </summary>
        /// <param name="receiptId"></param>
        /// <returns></returns>
        /// <history>
        /// 9/12/2014 aBc: create new
        /// </history>
        public ReceiptDetail Detail(int receiptId)
        {
            return (from receipt in _session.Receipts
                    where receipt.Id == receiptId
                    select new ReceiptDetail
                    {
                        Id = receipt.Id,
                        CustomerId = receipt.CustomerId,
                        Desciption = receipt.Desciption,
                        Payer = receipt.Payer,
                        CustomerName = receipt.Customer.Name,
                        ReceiptAmount = receipt.ReceiptAmount,
                        TotalAmount =  receipt.ReceiptAmount - receipt.AllowanceCharge,
                        ReceiptedDate = receipt.ReceiptedDate,
                        Receiver = receipt.Receiver,
                        StockOutId = receipt.StockOutId,
                    }).FirstOrDefault();
        }

        /// <summary>
        /// Get receipt detail
        /// </summary>
        /// <param name="documentNo"></param>
        /// <returns></returns>
        /// <history>
        /// 9/12/2014 aBc: create new
        /// </history>
        public ReceiptDetail Detail(string documentNo)
        {
            return (from receipt in _session.Receipts
                    where receipt.DocumentNo == documentNo
                    select new ReceiptDetail
                    {
                        Id = receipt.Id,
                        CustomerId = receipt.CustomerId,
                        Desciption = receipt.Desciption,
                        Payer = receipt.Payer,
                        CustomerName = receipt.Customer.Name,
                        ReceiptAmount = receipt.ReceiptAmount,
                        ReceiptedDate = receipt.ReceiptedDate,
                        Receiver = receipt.Receiver,
                        StockOutId = receipt.StockOutId,
                    }).FirstOrDefault();
        }
    }
}
